package com.hspedu.method;

/**
 * @author LiuJinTao
 * @className ThreadMethod
 * @date 2024/06/23 16:28
 * @description TODO
 */
public class ThreadMethod01 {
    public static void main(String[] args) throws InterruptedException {
        ThreadDemo1 threadDemo1 = new ThreadDemo1();
        threadDemo1.setName("老韩");
        threadDemo1.setPriority(Thread.MIN_PRIORITY);
        threadDemo1.start();

        // 主线程打印 5 个 hi ，然后中断子线程的休眠
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println("hi " + i);
        }
        System.out.println(threadDemo1.getName() + "线程的优先级是" + threadDemo1.getPriority());
        threadDemo1.interrupt();
    }
}


class ThreadDemo1 extends Thread {
    @Override
    public void run() {
        while (true) {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "   吃包子" + i);
            }
            try {
                System.out.println(Thread.currentThread().getName() + "   休眠中");
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                // 当该线程执行到一个 interrupt 方法时，就会 catch 一个异常，可以加入自己的业务代码
                System.out.println(Thread.currentThread().getName() + "被 interrupt 了");
            }
        }

    }
}